<template>
  <!--    :wrap="false" 关闭换行-->
  <a-row id="globalHeader" class="grid-demo" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        theme="dark"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/logo.jpg" />
            <div class="title">hua oj</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div
        v-if="
          (store.state.user?.loginUser?.userRole ?? ACCESS_ENUM.NOT_LOGIN) ===
          ACCESS_ENUM.NOT_LOGIN
        "
      >
        <router-link to="/user/login">
          {{ store.state.user?.loginUser?.userName ?? "未登录" }}
        </router-link>
      </div>
      <div v-else @click="logout">
        {{ store.state.user?.loginUser?.userName ?? "未登录" }}
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routers";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import ACCESS_ENUM from "@/access/ACCESS_ENUM";
import checkAccess from "@/access/checkAccess";
import { UserControllerService } from "../../generated";

const router = useRouter();
const store = useStore();

const logout = async () => {
  await UserControllerService.userLogoutUsingPost();
  store.dispatch("user/getLoginUser");
};

const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu === true) {
      return false;
    }
    if (
      !checkAccess(
        store.state.user.loginUser,
        (item?.meta?.access as string) ?? ACCESS_ENUM.NOT_LOGIN
      )
    ) {
      return false;
    }
    return true;
  });
});

// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "金花",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);

const selectedKeys = ref(["/"]);
// to跳转到那个路由 from从哪来 next实际跳转到哪个路由
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({ path: key });
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#globalHeader {
  margin-bottom: 16px;
}

.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 50px;
}
</style>
